﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using Acms.WebControls;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    public class RelatedPostsContentControl : BaseContentControlDisplay<RelatedPostsContentControlProperties>
    {
        protected override void CreateChildControls()
        {
            list = new Acms.WebControls.AcmsDataList();
            list.HeaderTemplate = this.HeaderTemplate;
            list.FooterTemplate = this.FooterTemplate;
            list.SeperatorTemplate = this.SeperatorTemplate;
            list.ItemTemplate = this.ItemTemplate;
            list.AlternatingItemTemplate = this.AlternatingItemTemplate;
            this.Controls.Add(list);
            base.CreateChildControls();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (list.Items != null && list.Items.Length > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
                if (!string.IsNullOrEmpty(CssClass))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                RenderItem(list.Header, writer);
                for (int i = 0; i < list.Items.Length; i++)
                {
                    RenderItem(list.Items[i], writer);
                    if (list.Seperators != null && list.Seperators.Length >= i)
                    {
                        RenderItem(list.Seperators[i], writer);
                    }
                }
                RenderItem(list.Footer, writer);
                writer.RenderEndTag();
            }
        }

        private void RenderItem(Acms.WebControls.AcmsDataListItem item, HtmlTextWriter writer)
        {
            if (item != null)
            {
                foreach (Control c in item.Controls)
                {
                    c.RenderControl(writer);
                }
            }
        }

        Acms.WebControls.AcmsDataList list;

        [
        Browsable(false),
        DefaultValue(null),
        Description("The header template."),
        TemplateContainer(typeof(AcmsDataListItem)),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public System.Web.UI.ITemplate HeaderTemplate { get; set; }

        [
        Browsable(false),
        DefaultValue(null),
        Description("The footer template."),
        TemplateContainer(typeof(AcmsDataListItem)),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public System.Web.UI.ITemplate FooterTemplate { get; set; }
        
        [
        Browsable(false),
        DefaultValue(null),
        Description("The sperator template."),
        TemplateContainer(typeof(AcmsDataListItem)),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public System.Web.UI.ITemplate SeperatorTemplate { get; set; }

        [
        Browsable(false),
        DefaultValue(null),
        Description("The item template."),
        TemplateContainer(typeof(AcmsDataListItem)),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public System.Web.UI.ITemplate ItemTemplate { get; set; }

        [
        Browsable(false),
        DefaultValue(null),
        Description("The alternating item template."),
        TemplateContainer(typeof(AcmsDataListItem)),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public System.Web.UI.ITemplate AlternatingItemTemplate { get; set; }

        protected override void DataBindChildren()
        {
            if (Properties != null)
            {
                list.DataSource = Properties.GetPosts(CurrentContext.SiteId, CurrentContext.SiteVersion);
                list.DataBind();
            }
        }

        public override bool IsEmpty
        {
            get 
            {
                if (Properties != null)
                {
                    return Properties.PostGuids.Length > 0;
                }
                else
                {
                    return true;
                }
            }
        }
    }
}
